#include "sinbullet.h"

namespace fireplane
{
	SinBullet::SinBullet(){

	}

	SinBullet::~SinBullet(){

	}

	void SinBullet::init(Bullet::BulletCommand* cmd){
		_body = (NULL);
		_lastZoneIdx = (-1);
		_currentZoneIdx = (-1);

		_speed = cmd->_speed;
		_dir = cmd->_dir;
		_owner = cmd->o;

		char buffer[0xff];
		sprintf(buffer,"%s","bullet/npc5/3.png");
		_body = CCSprite::create(buffer);
		float degree = CC_RADIANS_TO_DEGREES(acos(ccpDot(_dir,ccp(0,1))));

		if (ccpCross(_dir,ccp(0,1)) < 0)
		{
			degree = 360 - degree;
		}

		_body->setRotation(degree);
		_all_bullets_parent->addChild(this);
		this->addChild(_body);
		this->setPosition(cmd->_pos);

		_startPos = cmd->_pos;
		_startAngle = 0;
	}

	bool SinBullet::updateState(float dt){
		if (isDestroy())
		{
			return false;
		}

		CCSize winSize = CCDirector::sharedDirector()->getWinSize();
		CCRect rect = CCRectMake(0,0,winSize.width,winSize.height);

		CCPoint pos = this->getPosition();
		CCPoint worldpos = this->getParent()->convertToWorldSpace(pos);

		if (rect.containsPoint(worldpos))
		{
			_startAngle += 250 * dt;
			float moveY = _speed * dt;

			float angle = CC_DEGREES_TO_RADIANS(_startAngle);
			float moveX = 150 * sin(angle);

			CCPoint curpos;
			curpos.x = _startPos.x + (_dir.y * moveX);
			curpos.y = pos.y - moveY;
			this->setPosition(curpos);
			return true;
		}
		else
		{
			// out of screen
			return false;
		}
	}
}